﻿
using DriveManage.Tcp.Server;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;

namespace DriveManage.SocketClient
{

    public delegate void OnConnectedHandler(SocketClientManager scm);
    public delegate void OnReceiveMsgHandler(SocketClientManager scm, string Message);
    public delegate void ConnectErrorHandler(SocketClientManager scm, string ErrorMessage);

    // 定义一个委托，用于处理客户端连接事件的回调
    public delegate void ClientConnectedEventHandler(TcpClient client);
    // 定义一个委托，用于处理客户端断开连接事件的回调
    public delegate void ClientDisconnectedEventHandler(TcpClient client);
    // 定义一个委托，用于处理接收到客户端消息事件的回调
    public delegate void MessageReceivedEventHandler(SocketServerManager ServerManager, TcpClient client, byte[] buffer, string Message);

    public interface ISocketHandler
    {

        bool Start();

        /// <summary>
        /// 关闭连接
        /// </summary>
        void Disconnect();

        void SendMessage(string Message);

        bool IsConnected { get; }

        string Terminal { get; }

        StringBuilder ReceiveDataCache { get; }

        /// <summary>
        /// 客户端连接数
        /// </summary>
        List<TcpClient> ConnectedClients { get; }

        /// <summary>
        /// 重连机制
        /// </summary>
        void StartReconnect();

        event OnConnectedHandler OnConnected;
        event OnConnectedHandler OnFaildConnect;
        event OnReceiveMsgHandler OnReceiveMsg;
        event ConnectErrorHandler OnConnectError;

        /// <summary>
        /// 以下是服务端
        /// </summary>
        /// <returns></returns>
        bool Stop();

        bool HasConnectedClients();

        //void SendMessage(string Message);

        void SendMessage(TcpClient Client, string Message);

        //List<string> ReceiveDataCache { get; }

        event ClientConnectedEventHandler OnClientConnected;

        /// <summary>
        /// 客户端断开连接
        /// </summary>
        event ClientDisconnectedEventHandler OnClientDisconnected;

        /// <summary>
        /// 服务端接收客户端消息
        /// </summary>
        event MessageReceivedEventHandler OnMessageReceived;
    }
}
